# -*- coding: utf-8 -*-
'''
@文件    :monitor.py
@说明    :系统监控相关表
@时间    :2025/08/26 00:32:36
@作者    :浪晋
@版本    :1.0
'''

from tortoise import fields
from datetime import datetime, date
from decimal import Decimal
from .base import BaseModel, TimestampMixin, DefaultFilterManager


class MonitorLogModel(BaseModel, TimestampMixin):
    request_id = fields.CharField(max_length=50, null=False, description="请求Id")
    summary = fields.CharField(max_length=255, null=False, description="接口说明")
    url = fields.CharField(max_length=255, null=False, description="请求地址")
    method = fields.CharField(max_length=20, null=True, description="请求类型")
    client_ip = fields.CharField(max_length=20, null=True, description="客户端IP")
    headers = fields.TextField(null=True, description="请求头")
    payload = fields.JSONField(null=True, description="请求参数")
    result = fields.JSONField( null=True, description="响应结果")
    status_code = fields.CharField(max_length=20, null=True, description="响应码")
    process_time = fields.CharField(max_length=20, null=True, description="响应时间")
    error_log = fields.TextField(null=True, description="错误日志")
    class Meta:
        table = "tb_monitor_log"
        table_description = "系统日志表"
        manager = DefaultFilterManager()


class MonitorTaskModel(BaseModel, TimestampMixin):
    name = fields.CharField(max_length=50, null=False, description="任务名称")
    func = fields.CharField(max_length=50, null=False, description="执行方法")
    args = fields.JSONField(null=True, description="位置参数")
    kwargs = fields.JSONField(null=True, description="关键字参数")
    trigger = fields.CharField(max_length=50, null=False, description="触发器")
    executor = fields.CharField(max_length=50, null=False, description="执行器")
    jobstore = fields.CharField(max_length=50, null=False, description="储存器")
    start_date = fields.DatetimeField(null=True, description="开始时间")
    end_date = fields.DatetimeField(null=True, description="结束时间")
    trigger_args = fields.CharField(max_length=20, null=False, description="trigger表达式")
    active = fields.BooleanField(default=False, description="是否激活")
    desc = fields.CharField(max_length=255, null=True, description="任务描述")
    
    def to_dict(self, exclude: list[str] | None = None, include: dict | None = None):
        if exclude is None:
            exclude = []
        if include is None:
            include = {}
        exclude.append("deleted")
        result = {}
        result.update(include)
        for field in self._meta.db_fields:
            if field in exclude:
                continue
            value = getattr(self, field)
            if field == "args":
                value = ",".join(value)
            if field == "kwargs":
                value = "&".join([f"{k}={v}" for k, v in value.items()])
            if isinstance(value, datetime):
                value = value.strftime("%Y-%m-%d %H:%M:%S")
            if isinstance(value, date):
                value = value.strftime("%Y-%m-%d")
            if isinstance(value, Decimal):
                value = str(value)
            result[field] = value
        return result

    class Meta:
        table = "tb_monitor_task"
        table_description = "定时任务表"
        manager = DefaultFilterManager()